#ifndef _DriverPD_H_
#define _DriverPD_H_


typedef struct
{
    uint8_t  type               : 5;                /* 消息类型 */
    uint8_t  PortDataRole       : 1;                /* 对于SOP消息代表接口的功能角色（0-UFP; 1-DFP）； 对于SOP’和SOP’’保留 */
    uint8_t  version            : 2;                /* 此消息的当前版本（00-Rev1.0; 01-Rev2.0; 10-Rev3.0;） */
    uint8_t  PortPowerRole      : 1;                /* 对于SOP信息，代表接口的供电角色（0-Sink; 1-Source）； 对于SOP’或SOP’’信息，代表此消息是接口发送的还是线缆发送的 */
    uint8_t  ID                 : 3;                /* 消息ID,每发送一次，增加一次 */
    uint8_t  number             : 3;                /* 后面数据的个数，每个数据是4字节了，对于非扩展包，此字段的值为0~7 */
    uint8_t  extended           : 1;                /* 此消息是否为扩展类型 */
}PdMessHeadType;

/* https://blog.csdn.net/qq_43570266/article/details/134414268 */
typedef struct
{
    uint32_t maxCurrent         : 10;               /* Maximum Current in 10mA units */
    uint32_t voltage            : 10;               /* Voltage in 50mV units */
    uint32_t peakCurrent        : 2;                /* Peak Current */
    uint32_t reserved           : 1;                /* Reserved,shall be set to zero */
    uint32_t EprMode            : 1;                /* EPR Mode Capable */
    uint32_t UEMS               : 1;                /* Unchunked Extended Messages Supported */
    uint32_t dualRoleData       : 1;                /* Dual-Role Data */
    uint32_t usbComCapable      : 1;                /* USB Communications Capable */
    uint32_t unconPower         : 1;                /* Unconstrained Power */
    uint32_t suspendPower       : 1;                /* USB Suspend Power */
    uint32_t dualRolePower      : 1;                /* Dual-Role Power */
    uint32_t type               : 2;                /* Fixed Supply (must is 00) */
}PdSrcFixedType;

typedef struct
{
    uint32_t maxCurrent         : 10;               /* Maximum Current in 10mA units */
    uint32_t voltage            : 10;               /* Voltage in 50mV units */
    uint32_t reserved           : 3;                /* Reserved,shall be set to zero */
    uint32_t fastRoleSwap       : 2;                /* Fast Role Swap required USB Type-C Current(see also [USB Type-C2.3]) */
    uint32_t dualRoleData       : 1;                /* Dual-Role Data */
    uint32_t usbComCapable      : 1;                /* USB Communications Capable */
    uint32_t unconPower         : 1;                /* Unconstrained Power */
    uint32_t higherCapabi       : 1;                /* Higher Capability */
    uint32_t dualRolePower      : 1;                /* Dual-Role Power */
    uint32_t type               : 2;                /* Fixed Supply (must is 00) */
}PdSinkFixedType;

typedef struct
{
    uint32_t maxPower           : 10;               /* Maximum Allowable Power in 250mW units */
    uint32_t minVoltage         : 10;               /* Min Voltage in 50mV units */
    uint32_t maxVoltage         : 10;               /* Max Voltage in 50mV units */
    uint32_t type               : 2;                /* Battery Supply (must is 01) */
}PdSrcBatteryType;

typedef struct
{
    uint32_t operatPower        : 10;               /* Operational Power in 250mW units */
    uint32_t minVoltage         : 10;               /* Min Voltage in 50mV units */
    uint32_t maxVoltage         : 10;               /* Max Voltage in 50mV units */
    uint32_t type               : 2;                /* Battery Supply (must is 01) */
}PdSinkBatteryType;

typedef struct
{
    uint32_t maxCurrent         : 10;               /* Maximum Current in 10mA units */
    uint32_t minVoltage         : 10;               /* Min Voltage in 50mV units */
    uint32_t maxVoltage         : 10;               /* Max Voltage in 50mV units */
    uint32_t type               : 2;                /* Variable Supply (must is 10) */
}PdSrcVariableType;

typedef struct
{
    uint32_t operatCurrent      : 10;               /* Operational Current in 10mA units */
    uint32_t minVoltage         : 10;               /* Min Voltage in 50mV units */
    uint32_t maxVoltage         : 10;               /* Max Voltage in 50mV units */
    uint32_t type               : 2;                /* Variable Supply (must is 10) */
}PdSinkVariableType;

typedef struct
{
    uint32_t maxCurrent         : 7;                /* Maximum Current in 50mA units */
    uint32_t reserved0          : 1;                /* Reserved,shall be set to zero */
    uint32_t minVoltage         : 8;                /* Min Voltage in 100mV units */
    uint32_t reserved1          : 1;                /* Reserved,shall be set to zero */
    uint32_t maxVoltage         : 8;                /* Max Voltage in 100mV units */
    uint32_t reserved2          : 2;                /* Reserved,shall be set to zero */
    uint32_t powerLimited       : 1;                /* PPS Power Limited */
    uint32_t APDO               : 2;                /* SPR Programmbale Power Supply (must is 00) */
    uint32_t type               : 2;                /* SPR Programmable Supply (must is 11) */
}PdSrcSprProgramType;

typedef struct
{
    uint32_t inctements         : 8;                /* PDPin 1W increments */
    uint32_t minVoltage         : 8;                /* Min Voltage in 100mV units */
    uint32_t reserved           : 1;                /* Reserved,shall be set to zero */
    uint32_t maxVoltage         : 9;                /* Max Voltage in 100mV units */
    uint32_t peakCurrent        : 2;                /* Peak Current (see Table 6.15 "EPR AVS Power Source Peak Current Capability") */
    uint32_t APDO               : 2;                /* EPR Adjustable Voltage Supply (must is 01) */
    uint32_t type               : 2;                /* Augmented Power Data Object(APD0) (must is 11) */
}PdSrcEprAdjusType;

typedef struct
{
    uint32_t maxCurrent15V      : 10;               /* For 15V-20V range:Maximum Current in 10mA units equal to the Maximum Current field of the 20V Fixed Source PDO,set to 0 if the Maximum voltage in the SPR AVS range is 15V. */
    uint32_t maxCurrent9V       : 10;               /* For 9V-15V range:Maximum Current in 10mA units equal to the Maximum Current field of the 15V Fixed Source PDO */
    uint32_t reserved           : 6;                /* Reserved,shall be set to zero */
    uint32_t peakCurrent        : 2;                /* Peak Current (see Table 6.15 "EPR AVS Power Source Peak Current Capability") */
    uint32_t APDO               : 2;                /* SPR Adjustable Voltage Supply (must is 10) */
    uint32_t type               : 2;                /* Augmented Power Data Object(APD0) (must is 11) */
}PdSrcSprAdjusType;

typedef union
{
    /* SRC */
    PdSrcFixedType      fixed;                      /* Fixed Supply PDO - Source */
    PdSrcBatteryType    battery;                    /* Battery Supply PDO - Source */
    PdSrcVariableType   variable;                   /* Variable Supply (non-Battery) PDO - Source */
    PdSrcSprProgramType sprProgram;
    PdSrcEprAdjusType   eprAdjus;
    PdSrcSprAdjusType   sprAdjus;

    /* SINK */
    PdSinkFixedType     sinkFixed;                  /* Fixed Supply PDO - Sink */
    PdSinkBatteryType   sinkBattery;                /* Battery Supply PDO - Sink */
    PdSinkVariableType  sinkvariable;               /* Variable Supply (non-Battery) PDO - Sink */
}PdCapabiType;

/* 电源请求结构 */
typedef struct
{
    uint32_t  maxCurrent        : 10;               /* Maximum Operating Current in 10mA units */
    uint32_t  operatCurrent     : 10;               /* Operating current in 10mA units */
    uint32_t  reserved          : 2;                /* Reserved,shall be set to zero */
    uint32_t  EMC               : 1;                /* EPR Mode Capable */
    uint32_t  UEMS              : 1;                /* Unchunked Extended Message Supported */
    uint32_t  noUsbSusped       : 1;                /* No USB Susped */
    uint32_t  usbComCapable     : 1;                /* USB Communications Capable */
    uint32_t  capabiMismatch    : 1;                /* Capability Mismatch */
    uint32_t  giveBackFlag      : 1;                /* Give Back Flag */
    uint32_t  objectPosition    : 4;                /* Object Position */
}PdRequestCapabiType;

typedef struct
{
    PdMessHeadType head;
    uint8_t datas[28];
}PdMessStdType;

typedef struct
{
    PdMessHeadType head;
    uint8_t datas[260];
}PdMessExtType;

typedef struct
{
    PdCapabiType srcCapabi[7];                      /* PD 源端支持的电源参数 */
    uint8_t srcCapabNum         : 3;                /* PD 源端支持的电源参数 数量 */

    uint8_t connected           : 2;                /* USB接入状态，bit0:CC1; bit1:CC2 */
    uint8_t version             : 2;                /* 00-Rev1.0; 01-Rev2.0; 10-Rev3.0; */
    uint8_t PortDataRole        : 1;                /* 0-UFP; 1-DFP */
    uint8_t PortPowerRole       : 1;                /* 0-Sink; 1-Source */
    uint8_t ID                  : 3;                /* 消息ID,每发送一次，增加一次 */

    uint8_t status;                                 /* 状态（CC_STATUS）*/
    uint8_t statusLast;                             /* 状态（CC_STATUS）*/
    volatile uint32_t timeout;                      /* 状态起始时刻 */
}PdType;



int8_t cPdInit(void);
int8_t cPdDeinit(void);
int8_t cPdSrcInit(void);
int8_t cPdSinkInit(void);
void vPdCCSet(uint8_t ucChannel);
uint8_t ucPdCCDetect(void);
int8_t cPdRxMode(void);

int8_t cPdPackSend(uint8_t ucSop, uint8_t ucEx, uint8_t ucType, uint8_t ucID, void *pvDatas, int32_t iDataLength, int8_t cWaitCnt);
#define cPdPackControlSend(ucSop, ucType, ucID)                       cPdPackSend((ucSop), 0, (ucType), (ucID), NULL, 0, 0)
#define cPdPackDatasSend(ucSop, ucType, ucID, pvDatas, iDataLength)   cPdPackSend((ucSop), 0, (ucType), (ucID), (pvDatas), (iDataLength), 3)

int8_t cPdRequest(uint8_t ucIndex);

int8_t cPdPoll(void);
int8_t cPdMessRecive(PdMessStdType *ptypePdMess);


#endif
